//	Ordinaire.Database.DatabaseCollection
//	Copyright (c) 2008 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Ordinaire.Db
{
	/// <summary>
	/// This class holds a collection of IDatabase instances.
	/// </summary>
	/// <seealso cref="Ordinaire.Database.DatabaseFactory"/>
	public class DbCollection : Hashtable
	{
		#region Variables
		private Hashtable htDbProviders;
		#endregion

		#region Constructor
		/// <summary>
		/// Initializes a new instance of DatabaseCollection.
		/// </summary>
		public DbCollection()
		{
			// Uses Hashtable class to hold the collection
			htDbProviders = new Hashtable();
		}
		#endregion

		#region Public methods
		/// <summary>
		/// Adds a new instance of IDatabase class with the specified name.
		/// </summary>
		/// <param name="name">name</param>
		/// <param name="provider">IDatabase instance</param>
		public void Add(string name, IDatabase provider)
		{
			if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
			if (provider == null) throw new ArgumentNullException("provider");

			// Add instance into Hashtable
			htDbProviders.Add(name, provider);
		}

		/// <summary>
		/// Returns an instance of IDatabase class with a specified name.
		/// </summary>
		/// <param name="name">name</param>
		/// <returns>Returns null if the instance with the specified name does not exist in the colection.</returns>
		public IDatabase this[string name]
		{
			get
			{
				IDatabase provider = (IDatabase) htDbProviders[name];
				if (provider == null) { throw new KeyNotFoundException(name); }

				return (IDatabase) provider;
			}
		}
		#endregion
	}
}
